<template>
  <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
      :size="formSize"
  >
    <el-form-item label="Project name" prop="projectName">
      <el-input v-model="ruleForm.projectName"></el-input>
    </el-form-item>
    <el-form-item label="ParticleSize" prop="particleSize">
      <el-select v-model="ruleForm.particleSize" placeholder="particleSize">
        <el-option label="rough" value="rough"></el-option>
        <el-option label="detailed" value="detailed"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="Deadline" required>
      <el-col :span="11">
        <el-form-item prop="deadline">
          <el-date-picker
              v-model="ruleForm.deadline"
              type="date"
              placeholder="Pick a date"
              style="width: 100%"
          ></el-date-picker>
        </el-form-item>
      </el-col>
    </el-form-item>

    <el-form-item label="target" prop="target">
      <el-input v-model="ruleForm.target"></el-input>
    </el-form-item>
    <el-form-item label="fps" prop="fps" v-if="ruleForm.particleSize == detailed ">
      <el-radio-group v-model="ruleForm.fps">
        <el-radio label="4"></el-radio>
        <el-radio label="8"></el-radio>
      </el-radio-group>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)"
      >Create
      </el-button
      >
      <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import {reactive, ref} from 'vue'
import type {ElForm} from 'element-plus'
import {detailed} from "@/store/pojo/ParticleSize";
import createProject from "@/api/project/createProject";

type FormInstance = InstanceType<typeof ElForm>

let publisherId: number;
if (sessionStorage.getItem("user.id") != null) {
  publisherId = parseInt(sessionStorage.getItem("user.id").toString());
}
const formSize = ref('')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  publisherId,
  projectName: '',
  particleSize: '',
  target: [],
  deadline: Date,
  fps: 0,
})


const rules = reactive({
  projectName: [
    {
      required: true,
      message: 'Please input projectName',
      trigger: 'blur',
    },
    {
      min: 3,
      max: 25,
      message: 'Length should be 3 to 25',
      trigger: 'blur',
    },
  ],
  particleSize: [
    {
      required: true,
      message: 'Please select particleSize',
      trigger: 'change',
    },
  ],
  deadline: [
    {
      type: 'date',
      required: true,
      message: 'Please pick a deadline',
      trigger: 'change',
    },
  ],
  target: [
    {

      required: true,
      message: 'Please use "," to split different target',
      trigger: 'blur',
    },
  ],
  fps: [
    {
      required: true,
      message: 'Please select fps',
      trigger: 'change',
    },
  ],

})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      createProject(ruleForm);
    } else {
      console.log('error submit!')
      return false
    }
  })
}
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<style scoped>

</style>